<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width">
<meta name="theme-color" content="#222"><meta name="generator" content="Hexo 6.3.0">

  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png">
  <link rel="mask-icon" href="/images/logo.svg" color="#222">

<link rel="stylesheet" href="/css/main.css">



<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.2.1/css/all.min.css" integrity="sha256-Z1K5uhUaJXA7Ll0XrZ/0JhX4lAtZFpT6jkKrEDT0drU=" crossorigin="anonymous">
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/animate.css/3.1.1/animate.min.css" integrity="sha256-PR7ttpcvz8qrF57fur/yAx1qXMFJeJFiA6pSzWi0OIE=" crossorigin="anonymous">

<script class="next-config" data-name="main" type="application/json">{"hostname":"example.com","root":"/","images":"/images","scheme":"Muse","darkmode":false,"version":"8.14.1","exturl":false,"sidebar":{"position":"left","display":"post","padding":18,"offset":12},"copycode":{"enable":false,"style":null},"bookmark":{"enable":false,"color":"#222","save":"auto"},"mediumzoom":false,"lazyload":false,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"stickytabs":false,"motion":{"enable":true,"async":false,"transition":{"menu_item":"fadeInDown","post_block":"fadeIn","post_header":"fadeInDown","post_body":"fadeInDown","coll_header":"fadeInLeft","sidebar":"fadeInUp"}},"prism":false,"i18n":{"placeholder":"搜索...","empty":"没有找到任何搜索结果：${query}","hits_time":"找到 ${hits} 个搜索结果（用时 ${time} 毫秒）","hits":"找到 ${hits} 个搜索结果"},"path":"/search.xml","localsearch":{"enable":true,"trigger":"auto","top_n_per_article":-1,"unescape":false,"preload":false}}</script><script src="/js/config.js"></script>

    <meta name="description" content="基本操作左旋右旋12345678910111213141516171819&#x2F;&#x2F; 右旋void zig(Node *&amp;p) &amp;#123;    Node *q &#x3D; p-&gt;left;    p-&gt;left &#x3D; q-&gt;right;    q-&gt;right &#x3D; p;    p &#x3D; q;    pushUp(p-&gt;right);    pushUp(p);&amp;#125;&#x2F;">
<meta property="og:type" content="article">
<meta property="og:title" content="AVL与RedBlackTree">
<meta property="og:url" content="http://example.com/2021/07/27/AVL%E4%B8%8ERedBlackTree/index.html">
<meta property="og:site_name" content="JsyBlog">
<meta property="og:description" content="基本操作左旋右旋12345678910111213141516171819&#x2F;&#x2F; 右旋void zig(Node *&amp;p) &amp;#123;    Node *q &#x3D; p-&gt;left;    p-&gt;left &#x3D; q-&gt;right;    q-&gt;right &#x3D; p;    p &#x3D; q;    pushUp(p-&gt;right);    pushUp(p);&amp;#125;&#x2F;">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="http://example.com/2021/07/27/AVL%E4%B8%8ERedBlackTree/zig-zag.png">
<meta property="article:published_time" content="2021-07-26T16:00:00.000Z">
<meta property="article:modified_time" content="2021-12-16T06:25:55.585Z">
<meta property="article:author" content="SongyangJi">
<meta property="article:tag" content="数据结构">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="http://example.com/2021/07/27/AVL%E4%B8%8ERedBlackTree/zig-zag.png">


<link rel="canonical" href="http://example.com/2021/07/27/AVL%E4%B8%8ERedBlackTree/">



<script class="next-config" data-name="page" type="application/json">{"sidebar":"","isHome":false,"isPost":true,"lang":"zh-CN","comments":true,"permalink":"http://example.com/2021/07/27/AVL%E4%B8%8ERedBlackTree/","path":"2021/07/27/AVL与RedBlackTree/","title":"AVL与RedBlackTree"}</script>

<script class="next-config" data-name="calendar" type="application/json">""</script>
<title>AVL与RedBlackTree | JsyBlog</title>
  








  <noscript>
    <link rel="stylesheet" href="/css/noscript.css">
  </noscript>
</head>

<body itemscope itemtype="http://schema.org/WebPage" class="use-motion">
  <div class="headband"></div>

  <main class="main">
    <div class="column">
      <header class="header" itemscope itemtype="http://schema.org/WPHeader"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏" role="button">
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/" class="brand" rel="start">
      <i class="logo-line"></i>
      <p class="site-title">JsyBlog</p>
      <i class="logo-line"></i>
    </a>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger" aria-label="搜索" role="button">
        <i class="fa fa-search fa-fw fa-lg"></i>
    </div>
  </div>
</div>



<nav class="site-nav">
  <ul class="main-menu menu"><li class="menu-item menu-item-home"><a href="/" rel="section"><i class="fa fa-home fa-fw"></i>首页</a></li><li class="menu-item menu-item-tags"><a href="/tags/" rel="section"><i class="fa fa-tags fa-fw"></i>标签</a></li><li class="menu-item menu-item-categories"><a href="/categories/" rel="section"><i class="fa fa-th fa-fw"></i>分类</a></li><li class="menu-item menu-item-archives"><a href="/archives/" rel="section"><i class="fa fa-archive fa-fw"></i>归档</a></li>
      <li class="menu-item menu-item-search">
        <a role="button" class="popup-trigger"><i class="fa fa-search fa-fw"></i>搜索
        </a>
      </li>
  </ul>
</nav>



  <div class="search-pop-overlay">
    <div class="popup search-popup"><div class="search-header">
  <span class="search-icon">
    <i class="fa fa-search"></i>
  </span>
  <div class="search-input-container">
    <input autocomplete="off" autocapitalize="off" maxlength="80"
           placeholder="搜索..." spellcheck="false"
           type="search" class="search-input">
  </div>
  <span class="popup-btn-close" role="button">
    <i class="fa fa-times-circle"></i>
  </span>
</div>
<div class="search-result-container no-result">
  <div class="search-result-icon">
    <i class="fa fa-spinner fa-pulse fa-5x"></i>
  </div>
</div>

    </div>
  </div>

</header>
        
  
  <aside class="sidebar">

    <div class="sidebar-inner sidebar-nav-active sidebar-toc-active">
      <ul class="sidebar-nav">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <div class="sidebar-panel-container">
        <!--noindex-->
        <div class="post-toc-wrap sidebar-panel">
            <div class="post-toc animated"><ol class="nav"><li class="nav-item nav-level-1"><a class="nav-link" href="#%E5%9F%BA%E6%9C%AC%E6%93%8D%E4%BD%9C"><span class="nav-number">1.</span> <span class="nav-text">基本操作</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#%E5%B7%A6%E6%97%8B%E5%8F%B3%E6%97%8B"><span class="nav-number">1.1.</span> <span class="nav-text">左旋右旋</span></a></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#%E7%BA%A2%E9%BB%91%E6%A0%91"><span class="nav-number">2.</span> <span class="nav-text">红黑树</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#%E7%BA%A2%E9%BB%91%E6%A0%91%E7%9A%84%E6%80%A7%E8%B4%A8"><span class="nav-number">2.1.</span> <span class="nav-text">红黑树的性质</span></a></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#AVL"><span class="nav-number">3.</span> <span class="nav-text">AVL</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#%E5%9B%9B%E7%A7%8D%E6%83%85%E5%86%B5"><span class="nav-number">3.1.</span> <span class="nav-text">四种情况</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E6%B7%BB%E5%8A%A0%E6%AD%A5%E9%AA%A4"><span class="nav-number">3.2.</span> <span class="nav-text">添加步骤</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E5%88%A0%E9%99%A4%E6%AD%A5%E9%AA%A4"><span class="nav-number">3.3.</span> <span class="nav-text">删除步骤</span></a></li></ol></li></ol></div>
        </div>
        <!--/noindex-->

        <div class="site-overview-wrap sidebar-panel">
          <div class="site-author animated" itemprop="author" itemscope itemtype="http://schema.org/Person">
  <p class="site-author-name" itemprop="name">SongyangJi</p>
  <div class="site-description" itemprop="description"></div>
</div>
<div class="site-state-wrap animated">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
        <a href="/archives/">
          <span class="site-state-item-count">251</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
          <a href="/categories/">
        <span class="site-state-item-count">45</span>
        <span class="site-state-item-name">分类</span></a>
      </div>
      <div class="site-state-item site-state-tags">
          <a href="/tags/">
        <span class="site-state-item-count">109</span>
        <span class="site-state-item-name">标签</span></a>
      </div>
  </nav>
</div>

        </div>
      </div>
    </div>

    
  </aside>


    </div>

    <div class="main-inner post posts-expand">


  


<div class="post-block">
  
  

  <article itemscope itemtype="http://schema.org/Article" class="post-content" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="http://example.com/2021/07/27/AVL%E4%B8%8ERedBlackTree/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.gif">
      <meta itemprop="name" content="SongyangJi">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="JsyBlog">
      <meta itemprop="description" content="">
    </span>

    <span hidden itemprop="post" itemscope itemtype="http://schema.org/CreativeWork">
      <meta itemprop="name" content="AVL与RedBlackTree | JsyBlog">
      <meta itemprop="description" content="">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          AVL与RedBlackTree
        </h1>

        <div class="post-meta-container">
          <div class="post-meta">
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar"></i>
      </span>
      <span class="post-meta-item-text">发表于</span>

      <time title="创建时间：2021-07-27 00:00:00" itemprop="dateCreated datePublished" datetime="2021-07-27T00:00:00+08:00">2021-07-27</time>
    </span>
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar-check"></i>
      </span>
      <span class="post-meta-item-text">更新于</span>
      <time title="修改时间：2021-12-16 14:25:55" itemprop="dateModified" datetime="2021-12-16T14:25:55+08:00">2021-12-16</time>
    </span>
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-folder"></i>
      </span>
      <span class="post-meta-item-text">分类于</span>
        <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
          <a href="/categories/%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84/" itemprop="url" rel="index"><span itemprop="name">数据结构</span></a>
        </span>
    </span>

  
</div>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">
        <h1 id="基本操作"><a href="#基本操作" class="headerlink" title="基本操作"></a>基本操作</h1><h2 id="左旋右旋"><a href="#左旋右旋" class="headerlink" title="左旋右旋"></a>左旋右旋</h2><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 右旋</span></span><br><span class="line"><span class="function"><span class="type">void</span> <span class="title">zig</span><span class="params">(Node *&amp;p)</span> </span>&#123;</span><br><span class="line">    Node *q = p-&gt;left;</span><br><span class="line">    p-&gt;left = q-&gt;right;</span><br><span class="line">    q-&gt;right = p;</span><br><span class="line">    p = q;</span><br><span class="line">    <span class="built_in">pushUp</span>(p-&gt;right);</span><br><span class="line">    <span class="built_in">pushUp</span>(p);</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">// 左旋</span></span><br><span class="line"><span class="function"><span class="type">void</span> <span class="title">zag</span><span class="params">(Node *&amp;q)</span> </span>&#123;</span><br><span class="line">    Node *p = q-&gt;right;</span><br><span class="line">    q-&gt;right = p-&gt;left;</span><br><span class="line">    p-&gt;left = q;</span><br><span class="line">    q = p;</span><br><span class="line">    <span class="built_in">pushUp</span>(q-&gt;left);</span><br><span class="line">    <span class="built_in">pushUp</span>(q);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>





<img src="zig-zag.png" style="zoom:20%;" />









<h1 id="红黑树"><a href="#红黑树" class="headerlink" title="红黑树"></a>红黑树</h1><h2 id="红黑树的性质"><a href="#红黑树的性质" class="headerlink" title="红黑树的性质"></a>红黑树的性质</h2><ol>
<li>性质1：每个节点要么是黑色，要么是红色。</li>
<li>性质2：根节点是黑色。</li>
<li>性质3：每个叶子节点（NIL）是黑色。</li>
<li>性质4：每个红色结点的两个子结点一定都是黑色。</li>
<li>性质5：任意一结点到每个叶子结点的路径都包含数量相同的黑结点。</li>
</ol>
<p><strong>当然红黑树也必须是二叉搜索树。</strong></p>
<p>总结后的精华就是下面两点：</p>
<ol>
<li><strong>红黑树的性质是每条路径的黑色节点数目相同</strong>;</li>
<li><strong>红黑树保证最长路径不超过最短路径的二倍，因而近似平衡</strong>;</li>
</ol>
<h1 id="AVL"><a href="#AVL" class="headerlink" title="AVL"></a>AVL</h1><h2 id="四种情况"><a href="#四种情况" class="headerlink" title="四种情况"></a>四种情况</h2><h2 id="添加步骤"><a href="#添加步骤" class="headerlink" title="添加步骤"></a>添加步骤</h2><h2 id="删除步骤"><a href="#删除步骤" class="headerlink" title="删除步骤"></a>删除步骤</h2><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class="line">126</span><br><span class="line">127</span><br><span class="line">128</span><br><span class="line">129</span><br><span class="line">130</span><br><span class="line">131</span><br><span class="line">132</span><br><span class="line">133</span><br><span class="line">134</span><br><span class="line">135</span><br><span class="line">136</span><br><span class="line">137</span><br><span class="line">138</span><br><span class="line">139</span><br><span class="line">140</span><br><span class="line">141</span><br><span class="line">142</span><br><span class="line">143</span><br><span class="line">144</span><br><span class="line">145</span><br><span class="line">146</span><br><span class="line">147</span><br><span class="line">148</span><br><span class="line">149</span><br><span class="line">150</span><br><span class="line">151</span><br><span class="line">152</span><br><span class="line">153</span><br><span class="line">154</span><br><span class="line">155</span><br><span class="line">156</span><br><span class="line">157</span><br><span class="line">158</span><br><span class="line">159</span><br><span class="line">160</span><br><span class="line">161</span><br><span class="line">162</span><br><span class="line">163</span><br><span class="line">164</span><br><span class="line">165</span><br><span class="line">166</span><br><span class="line">167</span><br><span class="line">168</span><br><span class="line">169</span><br><span class="line">170</span><br><span class="line">171</span><br><span class="line">172</span><br><span class="line">173</span><br><span class="line">174</span><br><span class="line">175</span><br><span class="line">176</span><br><span class="line">177</span><br><span class="line">178</span><br><span class="line">179</span><br><span class="line">180</span><br><span class="line">181</span><br><span class="line">182</span><br><span class="line">183</span><br><span class="line">184</span><br><span class="line">185</span><br><span class="line">186</span><br><span class="line">187</span><br><span class="line">188</span><br><span class="line">189</span><br><span class="line">190</span><br><span class="line">191</span><br><span class="line">192</span><br><span class="line">193</span><br><span class="line">194</span><br><span class="line">195</span><br><span class="line">196</span><br><span class="line">197</span><br><span class="line">198</span><br><span class="line">199</span><br><span class="line">200</span><br><span class="line">201</span><br><span class="line">202</span><br><span class="line">203</span><br><span class="line">204</span><br><span class="line">205</span><br><span class="line">206</span><br><span class="line">207</span><br><span class="line">208</span><br><span class="line">209</span><br><span class="line">210</span><br><span class="line">211</span><br><span class="line">212</span><br><span class="line">213</span><br><span class="line">214</span><br><span class="line">215</span><br><span class="line">216</span><br><span class="line">217</span><br><span class="line">218</span><br><span class="line">219</span><br><span class="line">220</span><br><span class="line">221</span><br><span class="line">222</span><br><span class="line">223</span><br><span class="line">224</span><br><span class="line">225</span><br><span class="line">226</span><br><span class="line">227</span><br><span class="line">228</span><br><span class="line">229</span><br><span class="line">230</span><br><span class="line">231</span><br><span class="line">232</span><br><span class="line">233</span><br><span class="line">234</span><br><span class="line">235</span><br><span class="line">236</span><br><span class="line">237</span><br><span class="line">238</span><br><span class="line">239</span><br><span class="line">240</span><br><span class="line">241</span><br><span class="line">242</span><br><span class="line">243</span><br><span class="line">244</span><br><span class="line">245</span><br><span class="line">246</span><br><span class="line">247</span><br><span class="line">248</span><br><span class="line">249</span><br><span class="line">250</span><br><span class="line">251</span><br><span class="line">252</span><br><span class="line">253</span><br><span class="line">254</span><br><span class="line">255</span><br><span class="line">256</span><br><span class="line">257</span><br><span class="line">258</span><br><span class="line">259</span><br><span class="line">260</span><br><span class="line">261</span><br><span class="line">262</span><br><span class="line">263</span><br><span class="line">264</span><br><span class="line">265</span><br><span class="line">266</span><br><span class="line">267</span><br><span class="line">268</span><br><span class="line">269</span><br><span class="line">270</span><br><span class="line">271</span><br><span class="line">272</span><br><span class="line">273</span><br><span class="line">274</span><br><span class="line">275</span><br><span class="line">276</span><br><span class="line">277</span><br><span class="line">278</span><br><span class="line">279</span><br><span class="line">280</span><br><span class="line">281</span><br><span class="line">282</span><br><span class="line">283</span><br><span class="line">284</span><br><span class="line">285</span><br><span class="line">286</span><br><span class="line">287</span><br><span class="line">288</span><br><span class="line">289</span><br><span class="line">290</span><br><span class="line">291</span><br><span class="line">292</span><br><span class="line">293</span><br><span class="line">294</span><br><span class="line">295</span><br><span class="line">296</span><br><span class="line">297</span><br><span class="line">298</span><br><span class="line">299</span><br><span class="line">300</span><br><span class="line">301</span><br><span class="line">302</span><br><span class="line">303</span><br><span class="line">304</span><br><span class="line">305</span><br><span class="line">306</span><br><span class="line">307</span><br><span class="line">308</span><br><span class="line">309</span><br><span class="line">310</span><br><span class="line">311</span><br><span class="line">312</span><br><span class="line">313</span><br><span class="line">314</span><br><span class="line">315</span><br><span class="line">316</span><br><span class="line">317</span><br><span class="line">318</span><br><span class="line">319</span><br><span class="line">320</span><br><span class="line">321</span><br><span class="line">322</span><br><span class="line">323</span><br><span class="line">324</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//</span></span><br><span class="line"><span class="comment">// Created by SongyangJi on 2020/11/16.</span></span><br><span class="line"><span class="comment">//</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> *</span></span><br><span class="line"><span class="comment"> *  这个例子仅仅演示了AVL的基本操作，没有用泛型，只有键，没有值，</span></span><br><span class="line"><span class="comment"> *  并且假定所有数据都不相同。</span></span><br><span class="line"><span class="comment"> *</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;algorithm&gt;</span></span></span><br><span class="line"></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> std;</span><br><span class="line"></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&quot;binaryTree/BinarySearchTree.h&quot;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;vector&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;queue&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;iostream&gt;</span></span></span><br><span class="line"></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> std;</span><br><span class="line"></span><br><span class="line"><span class="keyword">class</span> <span class="title class_">AVL</span> &#123;</span><br><span class="line"><span class="keyword">private</span>:</span><br><span class="line">    </span><br><span class="line">    <span class="comment">// 内部节点</span></span><br><span class="line">    <span class="keyword">struct</span> <span class="title class_">Node</span> &#123;</span><br><span class="line">        <span class="type">int</span> value;</span><br><span class="line">        Node *left, *right;</span><br><span class="line">        <span class="type">int</span> height = <span class="number">1</span>; <span class="comment">// 高度</span></span><br><span class="line"></span><br><span class="line">        <span class="built_in">Node</span>(<span class="type">int</span> value, Node *left = <span class="literal">nullptr</span>, Node *right = <span class="literal">nullptr</span>)</span><br><span class="line">                : <span class="built_in">value</span>(value), <span class="built_in">left</span>(left), <span class="built_in">right</span>(right) &#123;&#125;</span><br><span class="line">    &#125;;</span><br><span class="line"></span><br><span class="line">    Node *root = <span class="literal">nullptr</span>;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line"></span><br><span class="line">    <span class="built_in">AVL</span>() &#123;&#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="type">bool</span> <span class="title">query</span><span class="params">(<span class="type">int</span> value)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="built_in">query</span>(root, value);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="type">void</span> <span class="title">insert</span><span class="params">(<span class="type">int</span> value)</span> </span>&#123;</span><br><span class="line">        <span class="built_in">insert</span>(root, value);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="type">void</span> <span class="title">remove</span><span class="params">(<span class="type">int</span> value)</span> </span>&#123;</span><br><span class="line">        <span class="built_in">remove</span>(root, value);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">//  层次遍历，用来简单的检查</span></span><br><span class="line">    <span class="function"><span class="type">void</span> <span class="title">displayLayerOrder</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        cout &lt;&lt; <span class="string">&quot;层次遍历:&quot;</span> &lt;&lt; endl;</span><br><span class="line">        queue&lt;Node *&gt; q;</span><br><span class="line">        q.<span class="built_in">push</span>(root);</span><br><span class="line">        <span class="keyword">while</span> (!q.<span class="built_in">empty</span>()) &#123;</span><br><span class="line">            <span class="type">int</span> size = q.<span class="built_in">size</span>();</span><br><span class="line">            <span class="keyword">while</span> (size--) &#123;</span><br><span class="line">                Node *p = q.<span class="built_in">front</span>();</span><br><span class="line">                q.<span class="built_in">pop</span>();</span><br><span class="line">                <span class="keyword">if</span> (p == <span class="literal">nullptr</span>) <span class="keyword">continue</span>;</span><br><span class="line">                cout &lt;&lt; p-&gt;value &lt;&lt; <span class="string">&quot;   &quot;</span>;</span><br><span class="line">                q.<span class="built_in">push</span>(p-&gt;left);</span><br><span class="line">                q.<span class="built_in">push</span>(p-&gt;right);</span><br><span class="line">            &#125;</span><br><span class="line">            cout &lt;&lt; endl;</span><br><span class="line">        &#125;</span><br><span class="line">        cout &lt;&lt; endl;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function">vector&lt;<span class="type">int</span>&gt; <span class="title">getInOrder</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        vector&lt;<span class="type">int</span>&gt; res;</span><br><span class="line">        <span class="built_in">inOrder</span>(root, res);</span><br><span class="line">        <span class="keyword">return</span> res;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="type">int</span> <span class="title">getHeight</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="built_in">h</span>(root);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">    <span class="keyword">virtual</span> ~<span class="built_in">AVL</span>() &#123;</span><br><span class="line">        <span class="built_in">helperDestructor</span>(root);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">private</span>:</span><br><span class="line"></span><br><span class="line"><span class="comment">//    // 求高度, 规定只有一个节点的高度为 1， 用于求平衡因子</span></span><br><span class="line"><span class="comment">//    int height(Node* t)&#123;</span></span><br><span class="line"><span class="comment">//        if(t == nullptr) return 0;</span></span><br><span class="line"><span class="comment">//        return max(height(t-&gt;left),height(t-&gt;right))+1;</span></span><br><span class="line"><span class="comment">//    &#125;</span></span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="type">int</span> <span class="title">h</span><span class="params">(Node *t)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> t ? t-&gt;height : <span class="number">0</span>;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 平衡因子</span></span><br><span class="line">    <span class="function"><span class="type">int</span> <span class="title">balance_factor</span><span class="params">(Node *t)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">if</span> (t == <span class="literal">nullptr</span>) <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">        <span class="keyword">return</span> <span class="built_in">h</span>(t-&gt;left) - <span class="built_in">h</span>(t-&gt;right);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 更新 p 操作</span></span><br><span class="line">    <span class="function"><span class="type">void</span> <span class="title">pushUp</span><span class="params">(Node *p)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">if</span> (p == <span class="literal">nullptr</span>) <span class="keyword">return</span>;</span><br><span class="line">        p-&gt;height = <span class="built_in">max</span>(<span class="built_in">h</span>(p-&gt;left), <span class="built_in">h</span>(p-&gt;right)) + <span class="number">1</span>;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 右旋</span></span><br><span class="line">    <span class="function"><span class="type">void</span> <span class="title">zig</span><span class="params">(Node *&amp;p)</span> </span>&#123;</span><br><span class="line">        Node *q = p-&gt;left;</span><br><span class="line">        p-&gt;left = q-&gt;right;</span><br><span class="line">        q-&gt;right = p;</span><br><span class="line">        p = q;</span><br><span class="line">        <span class="built_in">pushUp</span>(p-&gt;right);</span><br><span class="line">        <span class="built_in">pushUp</span>(p);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 左旋</span></span><br><span class="line">    <span class="function"><span class="type">void</span> <span class="title">zag</span><span class="params">(Node *&amp;q)</span> </span>&#123;</span><br><span class="line">        Node *p = q-&gt;right;</span><br><span class="line">        q-&gt;right = p-&gt;left;</span><br><span class="line">        p-&gt;left = q;</span><br><span class="line">        q = p;</span><br><span class="line">        <span class="built_in">pushUp</span>(q-&gt;left);</span><br><span class="line">        <span class="built_in">pushUp</span>(q);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">  </span><br><span class="line">    <span class="comment">/*</span></span><br><span class="line"><span class="comment">  	 * 下面为 AVL树的四种情况</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">  </span><br><span class="line">    <span class="comment">// LL型,右旋 左子树的左子树插入节点</span></span><br><span class="line">    <span class="function"><span class="type">void</span> <span class="title">LL</span><span class="params">(Node *&amp;t)</span> </span>&#123;</span><br><span class="line">        <span class="built_in">zig</span>(t);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">// RR,左旋 右子树的右子树插入节点</span></span><br><span class="line">    <span class="function"><span class="type">void</span> <span class="title">RR</span><span class="params">(Node *&amp;t)</span> </span>&#123;</span><br><span class="line">        <span class="built_in">zag</span>(t);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">// LR，先左旋后右旋, 新节点位于t的左子树的右子树</span></span><br><span class="line">    <span class="function"><span class="type">void</span> <span class="title">LR</span><span class="params">(Node *&amp;t)</span> </span>&#123;</span><br><span class="line">        <span class="built_in">zag</span>(t-&gt;left);</span><br><span class="line">        <span class="built_in">zig</span>(t);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">// RL, 先右旋后左旋, 新节点位于t的右子树的左子树</span></span><br><span class="line">    <span class="function"><span class="type">void</span> <span class="title">RL</span><span class="params">(Node *&amp;t)</span> </span>&#123;</span><br><span class="line">        <span class="built_in">zig</span>(t-&gt;right);</span><br><span class="line">        <span class="built_in">zag</span>(t);</span><br><span class="line">    &#125;</span><br><span class="line">  </span><br><span class="line">    <span class="comment">// 插入操作，不仅要找到插入的位置，还要进行旋转进行高度的平衡</span></span><br><span class="line">    <span class="function"><span class="type">void</span> <span class="title">insert</span><span class="params">(Node *&amp;t, <span class="type">int</span> value)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">if</span> (t == <span class="literal">nullptr</span>) &#123;</span><br><span class="line">            t = <span class="keyword">new</span> <span class="built_in">Node</span>(value);</span><br><span class="line">            <span class="keyword">return</span>;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="comment">// 插入完成之后，自下而上的进行调整高度</span></span><br><span class="line"></span><br><span class="line">        <span class="keyword">if</span> (value &lt; t-&gt;value) &#123; <span class="comment">// 向左插入</span></span><br><span class="line">            <span class="built_in">insert</span>(t-&gt;left, value);</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">            <span class="type">int</span> leftH = <span class="built_in">h</span>(t-&gt;left);</span><br><span class="line">            <span class="type">int</span> rightH = <span class="built_in">h</span>(t-&gt;right);</span><br><span class="line">            <span class="comment">// 向左边插入，只有可能是 leftH &gt; rightH</span></span><br><span class="line">            <span class="keyword">if</span> (leftH - rightH &gt; <span class="number">1</span>) &#123;</span><br><span class="line">                <span class="comment">// LL 型</span></span><br><span class="line">                <span class="keyword">if</span> (value &lt;= t-&gt;left-&gt;value) &#123;</span><br><span class="line">                    <span class="built_in">LL</span>(t);</span><br><span class="line">                    <span class="comment">// LR 型</span></span><br><span class="line">                &#125; <span class="keyword">else</span> <span class="keyword">if</span> (value &gt; t-&gt;left-&gt;value) &#123;</span><br><span class="line">                    <span class="built_in">LR</span>(t);</span><br><span class="line">                &#125;</span><br><span class="line">            &#125;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">        &#125; <span class="keyword">else</span> <span class="keyword">if</span> (value &gt; t-&gt;value) &#123; <span class="comment">// 向右插入</span></span><br><span class="line">            <span class="built_in">insert</span>(t-&gt;right, value);</span><br><span class="line"></span><br><span class="line">            <span class="type">int</span> leftH = <span class="built_in">h</span>(t-&gt;left);</span><br><span class="line">            <span class="type">int</span> rightH = <span class="built_in">h</span>(t-&gt;right);</span><br><span class="line">						<span class="comment">// 向右边插入，只有可能是 rightH &gt; leftH </span></span><br><span class="line">            <span class="keyword">if</span> (rightH - leftH &gt; <span class="number">1</span>) &#123;</span><br><span class="line">                <span class="comment">// RR型</span></span><br><span class="line">                <span class="keyword">if</span> (value &gt;= t-&gt;right-&gt;value) &#123;</span><br><span class="line">                    <span class="built_in">RR</span>(t);</span><br><span class="line">                    <span class="comment">// RL型</span></span><br><span class="line">                &#125; <span class="keyword">else</span> <span class="keyword">if</span> (value &lt; t-&gt;right-&gt;value) &#123;</span><br><span class="line">                    <span class="built_in">RL</span>(t);</span><br><span class="line">                &#125;</span><br><span class="line"></span><br><span class="line">            &#125;</span><br><span class="line"></span><br><span class="line">        &#125;</span><br><span class="line">        <span class="comment">// 自底向上更新 t 的高度</span></span><br><span class="line">        <span class="built_in">pushUp</span>(t);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="type">void</span> <span class="title">remove</span><span class="params">(Node *&amp;p, <span class="type">int</span> value)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">if</span> (p == <span class="literal">nullptr</span>) <span class="keyword">return</span>;</span><br><span class="line">        <span class="keyword">if</span> (value &lt; p-&gt;value) &#123;</span><br><span class="line"></span><br><span class="line">            <span class="built_in">remove</span>(p-&gt;left, value);</span><br><span class="line">            <span class="comment">// 删除左子树的节点，唯一可能导致&quot;失衡&quot; 的情况是 bf由 -1 变成-2</span></span><br><span class="line">            <span class="type">int</span> bf = <span class="built_in">balance_factor</span>(p);</span><br><span class="line">            <span class="keyword">if</span> (bf &lt; <span class="number">-1</span>) &#123;</span><br><span class="line">                <span class="keyword">if</span> (<span class="built_in">h</span>(p-&gt;right-&gt;right) &gt;= <span class="built_in">h</span>(p-&gt;right-&gt;left)) &#123;</span><br><span class="line">                    <span class="built_in">RR</span>(p);</span><br><span class="line">                &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">                    <span class="built_in">RL</span>(p);</span><br><span class="line">                &#125;</span><br><span class="line">            &#125;</span><br><span class="line"></span><br><span class="line">        &#125; <span class="keyword">else</span> <span class="keyword">if</span> (value &gt; p-&gt;value) &#123;</span><br><span class="line"></span><br><span class="line">            <span class="built_in">remove</span>(p-&gt;right, value);</span><br><span class="line">            <span class="comment">// 删除右子树的节点，唯一可能导致&quot;失衡&quot; 的情况是 bf由 1 变成 2</span></span><br><span class="line">            <span class="type">int</span> bf = <span class="built_in">balance_factor</span>(p);</span><br><span class="line">            <span class="keyword">if</span> (bf &gt; <span class="number">1</span>) &#123;</span><br><span class="line">                <span class="keyword">if</span> (<span class="built_in">h</span>(p-&gt;left-&gt;left) &gt;= <span class="built_in">h</span>(p-&gt;left-&gt;right)) &#123;</span><br><span class="line">                    <span class="built_in">LL</span>(p);</span><br><span class="line">                &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">                    <span class="built_in">LR</span>(p);</span><br><span class="line">                &#125;</span><br><span class="line">            &#125;</span><br><span class="line"></span><br><span class="line">        &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">            <span class="comment">// 下面细分成 3种情况 (左右子树都为空，一棵为空另一棵不为空，都不为空)</span></span><br><span class="line"></span><br><span class="line">            <span class="keyword">if</span> (p-&gt;left == <span class="literal">nullptr</span> &amp;&amp; p-&gt;right == <span class="literal">nullptr</span>) &#123;</span><br><span class="line">                <span class="keyword">delete</span> p;</span><br><span class="line">                p = <span class="literal">nullptr</span>;</span><br><span class="line">            &#125; <span class="keyword">else</span> <span class="keyword">if</span> (p-&gt;left != <span class="literal">nullptr</span> &amp;&amp; p-&gt;right == <span class="literal">nullptr</span>) &#123;</span><br><span class="line">                Node *temp = p-&gt;left;</span><br><span class="line">                <span class="keyword">delete</span> p;</span><br><span class="line">                p = temp;</span><br><span class="line">            &#125; <span class="keyword">else</span> <span class="keyword">if</span> (p-&gt;left == <span class="literal">nullptr</span> &amp;&amp; p-&gt;right != <span class="literal">nullptr</span>) &#123;</span><br><span class="line">                Node *temp = p-&gt;right;</span><br><span class="line">                <span class="keyword">delete</span> p;</span><br><span class="line">                p = temp;</span><br><span class="line">            &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">                <span class="comment">// 用前驱的值代替（后继也是一样）</span></span><br><span class="line">                Node *cur = p-&gt;left;</span><br><span class="line">                <span class="keyword">while</span> (cur-&gt;right != <span class="literal">nullptr</span>) &#123;</span><br><span class="line">                    cur = cur-&gt;right;</span><br><span class="line">                &#125;</span><br><span class="line"></span><br><span class="line">                p-&gt;value = cur-&gt;value;</span><br><span class="line"></span><br><span class="line">                <span class="built_in">remove</span>(p-&gt;left, cur-&gt;value);</span><br><span class="line"></span><br><span class="line">                <span class="comment">// 这个地方仍然要有形态的调整</span></span><br><span class="line"></span><br><span class="line">                <span class="comment">// 删除左子树的节点，唯一可能导致&quot;失衡&quot; 的情况是 bf由 -1 变成-2</span></span><br><span class="line">                <span class="type">int</span> bf = <span class="built_in">balance_factor</span>(p);</span><br><span class="line">                <span class="keyword">if</span> (bf &lt; <span class="number">-1</span>) &#123;</span><br><span class="line">                    <span class="keyword">if</span> (<span class="built_in">h</span>(p-&gt;right-&gt;right) &gt;= <span class="built_in">h</span>(p-&gt;right-&gt;left)) &#123;</span><br><span class="line">                        <span class="built_in">RR</span>(p);</span><br><span class="line">                    &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">                        <span class="built_in">RL</span>(p);</span><br><span class="line">                    &#125;</span><br><span class="line">                &#125;</span><br><span class="line"></span><br><span class="line">            &#125;</span><br><span class="line">            <span class="comment">// 自底向上更新 t 的高度</span></span><br><span class="line">            <span class="built_in">pushUp</span>(p);</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="type">bool</span> <span class="title">query</span><span class="params">(Node *p, <span class="type">int</span> value)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">if</span> (p == <span class="literal">nullptr</span>) <span class="keyword">return</span> <span class="literal">false</span>;</span><br><span class="line">        <span class="keyword">if</span> (value &lt; p-&gt;value) &#123;</span><br><span class="line">            <span class="keyword">return</span> <span class="built_in">query</span>(p-&gt;left, value);</span><br><span class="line">        &#125; <span class="keyword">else</span> <span class="keyword">if</span> (value &gt; p-&gt;value) &#123;</span><br><span class="line">            <span class="keyword">return</span> <span class="built_in">query</span>(p-&gt;right, value);</span><br><span class="line">        &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">            <span class="keyword">return</span> <span class="literal">true</span>;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="type">void</span> <span class="title">helperDestructor</span><span class="params">(Node *node)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">if</span> (node == <span class="literal">nullptr</span>) <span class="keyword">return</span>;</span><br><span class="line">        <span class="built_in">helperDestructor</span>(node-&gt;left);</span><br><span class="line">        <span class="built_in">helperDestructor</span>(node-&gt;right);</span><br><span class="line">        <span class="keyword">delete</span> node;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="type">void</span> <span class="title">inOrder</span><span class="params">(Node *node, vector&lt;<span class="type">int</span>&gt; &amp;v)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">if</span> (node == <span class="literal">nullptr</span>) <span class="keyword">return</span>;</span><br><span class="line"></span><br><span class="line">        <span class="built_in">inOrder</span>(node-&gt;left, v);</span><br><span class="line">        v.<span class="built_in">push_back</span>(node-&gt;value);</span><br><span class="line">        <span class="built_in">inOrder</span>(node-&gt;right, v);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="type">int</span> <span class="title">main</span><span class="params">()</span> </span>&#123;</span><br><span class="line">    AVL avl;</span><br><span class="line">    <span class="keyword">for</span> (<span class="type">int</span> x = <span class="number">1</span>; x &lt;= <span class="number">15</span>; x++) &#123;</span><br><span class="line">        avl.<span class="built_in">insert</span>(x);</span><br><span class="line">    &#125;</span><br><span class="line">    vector&lt;<span class="type">int</span>&gt; v = &#123;<span class="number">11</span>,<span class="number">14</span>,<span class="number">13</span>,<span class="number">15</span>,<span class="number">9</span>,<span class="number">2</span>,<span class="number">3</span>,<span class="number">1</span>,<span class="number">6</span>,<span class="number">5</span>,<span class="number">7</span>&#125;;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">for</span>(<span class="type">int</span> x:v)&#123;</span><br><span class="line">        avl.<span class="built_in">remove</span>(x);</span><br><span class="line">        avl.<span class="built_in">displayLayerOrder</span>();</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre></td></tr></table></figure>






<blockquote>
<p>参考链接</p>
<p><a target="_blank" rel="noopener" href="https://www.cnblogs.com/skywang12345/p/3245399.html">红黑树(一)之 原理和算法详细介绍</a></p>
</blockquote>

    </div>

    
    
    

    <footer class="post-footer">
          <div class="post-tags">
              <a href="/tags/%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84/" rel="tag"># 数据结构</a>
          </div>

        

          <div class="post-nav">
            <div class="post-nav-item">
                <a href="/2021/07/26/Queue%E7%9A%84%E5%AE%9E%E7%8E%B0%E7%B1%BB%E6%BA%90%E7%A0%81%E5%88%86%E6%9E%90%E2%80%94%E2%80%94ArrayDeque%E3%80%81PriorityQueue/" rel="prev" title="Queue的实现类源码分析——ArrayDeque、PriorityQueue">
                  <i class="fa fa-chevron-left"></i> Queue的实现类源码分析——ArrayDeque、PriorityQueue
                </a>
            </div>
            <div class="post-nav-item">
                <a href="/2021/07/30/TreeMap%E3%80%81TreeSet%E6%BA%90%E7%A0%81/" rel="next" title="TreeMap、TreeSet源码">
                  TreeMap、TreeSet源码 <i class="fa fa-chevron-right"></i>
                </a>
            </div>
          </div>
    </footer>
  </article>
</div>






</div>
  </main>

  <footer class="footer">
    <div class="footer-inner">


<div class="copyright">
  &copy; 
  <span itemprop="copyrightYear">2023</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">SongyangJi</span>
</div>
  <div class="powered-by">由 <a href="https://hexo.io/" rel="noopener" target="_blank">Hexo</a> & <a href="https://theme-next.js.org/muse/" rel="noopener" target="_blank">NexT.Muse</a> 强力驱动
  </div>

    </div>
  </footer>

  
  <div class="toggle sidebar-toggle" role="button">
    <span class="toggle-line"></span>
    <span class="toggle-line"></span>
    <span class="toggle-line"></span>
  </div>
  <div class="sidebar-dimmer"></div>
  <div class="back-to-top" role="button" aria-label="返回顶部">
    <i class="fa fa-arrow-up fa-lg"></i>
    <span>0%</span>
  </div>

<noscript>
  <div class="noscript-warning">Theme NexT works best with JavaScript enabled</div>
</noscript>


  
  <script src="https://cdnjs.cloudflare.com/ajax/libs/animejs/3.2.1/anime.min.js" integrity="sha256-XL2inqUJaslATFnHdJOi9GfQ60on8Wx1C2H8DYiN1xY=" crossorigin="anonymous"></script>
<script src="/js/comments.js"></script><script src="/js/utils.js"></script><script src="/js/motion.js"></script><script src="/js/schemes/muse.js"></script><script src="/js/next-boot.js"></script>

  <script src="https://cdnjs.cloudflare.com/ajax/libs/hexo-generator-searchdb/1.4.1/search.js" integrity="sha256-1kfA5uHPf65M5cphT2dvymhkuyHPQp5A53EGZOnOLmc=" crossorigin="anonymous"></script>
<script src="/js/third-party/search/local-search.js"></script>





  





</body>
</html>
